<?php
//**************************************************************************************************
//==================================================================================================
//**************************************************************************************************
function createDir($dir) {
	if (!file_exists($dir)) { 
		mkdir($dir, 0755); 
	}
}

function form_load($msg = "", $tipo = "") {
	global $form_name;
	$txtCompany = "";
	$txtModuleName = "";
	echo "<h4>" . montaLink (_MI_MOD_DNN_GENERATOR, METODO_PADRAO, FORM_PADRAO) . " :: " . montaLink (_MI_MOD_DNN_GENERATOR_MENU0) . "</h4>";
	if ($msg != '') {
		if ($tipo == "S") { //sucesso		
			xoops_result($msg, _MI_MOD_CAPTION_MSG_SUCESS);
			unset($_POST);
		} else if ($tipo == "E") { //erro
			xoops_error($msg, _MI_MOD_CAPTION_MSG_ERR);
			$txtNome = $_POST['txtNome'];
		}
	}
	xoops_load('xoopsformloader');	
	$form = new XoopsThemeForm(_MI_MOD_DNN_GENERATOR_MENU0, $form_name . '_form', 'index.php', "post", true);
	$form->addElement(new XoopsFormHidden(FORM_PARAM, $form_name));
	$form->addElement(new XoopsFormHidden(FUNCTION_PARAM, 'exec'));
	//==================================================================================
	$form->addElement(new XoopsFormText(_MI_MOD_DNN_GENERATOR_COMPANY, 'txtCompany', 30, 30, $txtCompany), true);	
	$form->addElement(new XoopsFormText(_MI_MOD_DNN_GENERATOR_MODULENAME, 'txtModuleName', 30, 30, $txtModuleName), true);	
	//==================================================================================	
	$form->addElement(new XoopsFormButton('', $form_name . '_submit', _SUBMIT, 'submit'));
	$form->display();
}
function form_exec() {
	global $txtCompany, $txtModuleName;

	$msg="";
	$tipo="E";//erro
	
    if ($GLOBALS['xoopsSecurity']->check()) {
		if ($txtCompany == '') {
			$msg  = _MI_MOD_DNN_GENERATOR_COMPANY_OBRIGATORIO;
		} else if ($txtModuleName == '') {
			$msg  = _MI_MOD_DNN_GENERATOR_MODULENAME_OBRIGATORIO;
		} else {
			createDir(XOOPS_ROOT_PATH . "/modules/geradorDNN/temp");

			include_once XOOPS_ROOT_PATH.'/class/template.php';
			$tpl = new XoopsTpl();
   			$tpl->assign('txtCompany', $txtCompany);
   			$tpl->assign('txtModuleName', $txtModuleName);				
		
			require_once(XOOPS_ROOT_PATH.'/class/zipdownloader.php');
            $downloader = new XoopsZipDownloader();
			
			$downloader->addFileData($tpl->fetch('db:geradorDNN_01.00.00.SqlDataProvider.html'), "01.00.00.SqlDataProvider", time());
			$downloader->addFileData($tpl->fetch('db:geradorDNN_DataProvider.vb.html'), "DataProvider.vb", time());
			$downloader->addFileData($tpl->fetch('db:geradorDNN_EditModuleName.ascx.html'), "EditModuleName.ascx", time());
			$downloader->addFileData($tpl->fetch('db:geradorDNN_EditModuleName.ascx.resx.html'), "EditModuleName.ascx.resx", time());
			
			$downloader->addFileData($tpl->fetch('db:geradorDNN_EditModuleName.ascx.vb.html'), "EditModuleName.ascx.vb", time());
			$downloader->addFileData($tpl->fetch('db:geradorDNN_ModuleName.dnn.html'), "ModuleName.dnn", time());
			$downloader->addFileData($tpl->fetch('db:geradorDNN_ModuleNameController.vb.html'), "ModuleNameController.vb", time());
			$downloader->addFileData($tpl->fetch('db:geradorDNN_ModuleNameInfo.vb.html'), "ModuleNameInfo.vb", time());
			
			$downloader->addFileData($tpl->fetch('db:geradorDNN_Settings.ascx.html'), "Settings.ascx", time());
			$downloader->addFileData($tpl->fetch('db:geradorDNN_Settings.ascx.resx.html'), "Settings.ascx.resx", time());
			$downloader->addFileData($tpl->fetch('db:geradorDNN_Settings.ascx.vb.html'), "Settings.ascx.vb", time());
			$downloader->addFileData($tpl->fetch('db:geradorDNN_SqlDataProvider.vb.html'), "SqlDataProvider.vb", time());
			
			$downloader->addFileData($tpl->fetch('db:geradorDNN_Uninstall.SqlDataProvider.html'), "Uninstall.SqlDataProvider", time());
			$downloader->addFileData($tpl->fetch('db:geradorDNN_ViewModuleName.ascx.html'), "ViewModuleName.ascx", time());
			$downloader->addFileData($tpl->fetch('db:geradorDNN_ViewModuleName.ascx.resx.html'), "ViewModuleName.ascx.resx", time());
			$downloader->addFileData($tpl->fetch('db:geradorDNN_ViewModuleName.ascx.vb.html'), "ViewModuleName.ascx.vb", time());
						
			echo $downloader->download($txtModuleName, false);	

			$msg  = _MI_MOD_DNN_GENERATOR_SUCESS;
			$tipo = "S";//sucess
		}
	}
	form_load($msg, $tipo);
}
//**************************************************************************************************
//==================================================================================================
//**************************************************************************************************
?>